define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Stest, $window, $timeout, GoPage, GoDialog, GoPicture, FileSystem) {
		$scope.input = new Object();
		var invoice = new Object();
		var shipping = new Object();
		$scope.get_post = function() {
			console.log(1);
			Stest.get_url(function(data) {
				//		 		if(data.sucss)
			})
		};
		$scope.get_data = function() {
			Stest.get_data(function(data) {
				$scope.check = data;
			})
		};
		$scope.get_order = function() {
				invoice = new Object();
				shipping = new Object();
				$scope.input.receiver = JSON.stringify($scope.check.delivery[0]);
				for(var i = 0; i < $scope.check.order_items.length; i++) {
					shipping[$scope.check.order_items[i].store_id] = new Object();
					shipping[$scope.check.order_items[i].store_id][800] = new Object();
					shipping[$scope.check.order_items[i].store_id][800].shipping_id = $scope.check.order_items[i].shipping[0].id;
					shipping[$scope.check.order_items[i].store_id][800].memo = 'SDDF';
					invoice[$scope.check.order_items[i].store_id] = new Object();
					invoice[$scope.check.order_items[i].store_id][800] = new Object();
					invoice[$scope.check.order_items[i].store_id][800].type = 'no';
					invoice[$scope.check.order_items[i].store_id][800].tax_company = '';
					//				coupon_id[i] = $scope.check.order_items[i].coupon_id;
					//				shipping_id[i] = $scope.check.order_items[i].shipping_id;
					//				invoice_id[i] = $scope.check.order_items[i].invoice_id;
				}
				//			$scope.input.coupon_id = JSON.stringify(coupon_id);
				$scope.input.shipping = JSON.stringify(shipping);
				$scope.input.invoice = JSON.stringify(invoice);
				console.log($scope.input.receiver);
				console.log($scope.input.shipping);
				console.log($scope.input.invoice);
				var param = new Object();
				param.receiver = $scope.input.receiver;
				param.shipping = $scope.input.shipping;
				param.invoice = $scope.input.invoice;
				param.md5_cart_info = $scope.check.md5_cart_info;
				param.purchase = JSON.stringify($scope.check.purchase);
				param.extends_args = $scope.check.extends_args;
				param.split_order = $scope.check.split_order;
				param.business_goods = $scope.check.business_goods;
				console.log(param);
				Stest.create_pay(param, 12, function(data) {

				})
			},
			$scope.get_goods = function() {
				Stest.get_goods(function(data) {

				})
			}
		$scope.get_grouporder = function() {
			Stest.get_grouporder(function(data) {

			})
		}
		$scope.go_afsrue = function() {
			GoPage.go_states("afsrue", {
				"type": 'groupbuy',
				"status": 'fastbuy'
			})
		}
		$scope.get_list = function() {
			Stest.get_notice(function(data) {

			})
		}
		$scope.get_detail = function() {
			Stest.get_newsend(function(data) {

			})
		}
		$scope.doShowFile = function() {
			document.getElementById("upfilebutton").click();
		}
		$scope.onupload = function(val) {
			console.log(val);
			var fileList = val.files;
			for(var i = 0; i < val.files.length; i++) {
				var reader = new FileReader();
				reader.readAsDataURL(fileList[i]);
				console.log(fileList[i]);
				var filename = fileList[i].name;
				$scope.result = '';
				reader.onload = function(e) {
					$scope.result = e.target.result;
					$scope.file_name = filename;
				}
				reader.onloadend = function(e) {}
				$timeout(function() {
					$scope.send_file($scope.result, filename);
					//						$scope.get_paper();
					//						GoDialog.alert('提示','1234');
				}, 1000);
			}

		};
		$scope.send_file = function(file, filename) {
			console.log(HTTP_URL);
			Stest.upload(file, filename, function(data) {
				console.log(data);
				//			$scope.href = 'localhost:8092'+data.file_herf;
				//			console.log($scope.href);
			})
		}

		$scope.down = function(fileEntry, url) { 
			var fileTransfer = new FileTransfer();            
			var fileURL = fileEntry.toURL();             
			fileTransfer.download( uri,fileURL,function(entry) {                    
					console.log("下载成功！");                    
					alert("文件保存位置: " + entry.toURL());                
				},                 function(error) {                    
					alert("下载失败！");                    
					console.log("error source " + error.source);                    
					console.log("error target " + error.target);                    
					console.log("error code" + error.code);                
				},                 null, // or, pass false
				                 {                     //headers: {
					                     //    "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
					                     //}
					                }            );
		};
		$scope.download = function() { 
			window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {               
				console.log('打开的文件系统: ' + fs.name);              
				var url = 'http://192.168.1.73:8092/index.php/importexport-queue_download-64.html';              
				fs.root.getFile('12345.xls', {
					create: true,
					exclusive: false
				},               function(fileEntry) {                  
					$scope.down(fileEntry, url);              
				}, onErrorCreateFile);             
			}, onErrorLoadFs);
		}
		//文件创建失败回调
		function onErrorCreateFile(error) {
			alert.log("文件创建失败！")
		}

		//FileSystem加载失败回调
		function onErrorLoadFs(error) {
			alert.log("文件系统加载失败！")
		}

		//将内容数据写入到文件中
		$scope.writeFile = function(fileEntry, dataObj) {
			//创建一个写入对象
			fileEntry.createWriter(function(fileWriter) {

				//文件写入成功
				fileWriter.onwriteend = function() {
					alert("Successful file read...");
				};

				//文件写入失败
				fileWriter.onerror = function(e) {
					alert("Failed file read: " + e.toString());
				};

				//写入文件
				fileWriter.write(dataObj);
			});
		}
		$scope.file_download = function() {
			Stest.download(function(data) {
				$scope.data = $scope.decode(data);
				window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {

					console.log('打开的文件系统: ' + fs.name);
					fs.root.getFile("hangge.xls", { create: true, exclusive: false },
						function(fileEntry) {

							console.log("是否是个文件？" + fileEntry.isFile.toString());
							// fileEntry.name == 'hangge.txt'
							// fileEntry.fullPath == '/hangge.txt'
							//文件内容
							var dataObj = new Blob([$scope.data], { type: 'xls/plain' });
							//写入文件
							$scope.writeFile(fileEntry, null);

						}, onErrorCreateFile);

				}, onErrorLoadFs);
			})
		}
		var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
		$scope.decode = function(input) {
			var output = "";
			var chr1, chr2, chr3;
			var enc1, enc2, enc3, enc4;
			var i = 0;
			input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
			while(i < input.length) {
				enc1 = keyStr.indexOf(input.charAt(i++));
				enc2 = keyStr.indexOf(input.charAt(i++));
				enc3 = keyStr.indexOf(input.charAt(i++));
				enc4 = keyStr.indexOf(input.charAt(i++));
				chr1 = (enc1 << 2) | (enc2 >> 4);
				chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
				chr3 = ((enc3 & 3) << 6) | enc4;
				output = output + String.fromCharCode(chr1);
				if(enc3 != 64) {
					output = output + String.fromCharCode(chr2);
				}
				if(enc4 != 64) {
					output = output + String.fromCharCode(chr3);
				}
			}
			output = $scope.utf8_decode(output);
			return output;
		}
		$scope.utf8_decode = function(utftext) {
			var string = "";
			var i = 0;
			var c = 0;
			var c1 = 0;
			var c2 = 0;
			var c3 = 0;
			while(i < utftext.length) {
				c = utftext.charCodeAt(i);
				if(c < 128) {
					string += String.fromCharCode(c);
					i++;
				} else if((c > 191) && (c < 224)) {
					c2 = utftext.charCodeAt(i + 1);
					string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
					i += 2;
				} else {
					c2 = utftext.charCodeAt(i + 1);
					c3 = utftext.charCodeAt(i + 2);
					string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
					i += 3;
				}
			}
			return string;
		}
		$scope.down = function() {
						Stest.download(function(data){
						GoDialog.alert('tishui',data.result.files);
						});
//			FileSystem.downloadFile(55, function(data) {
//				alert('下载成功');
//			}, function(e) {
//				alert(下载失败);
//			})
		}
		$scope.doShowSystem = function() {

			GoPage.go_states("filesystem", {});
		}
		//分享测试
		$scope.sharetest = function() {

		};
		//生成对象
		$scope.makeplayer = function(token) {
			var opts = {
				"playerId": "gotyeswf", //生成视频dom对象id,可为空,默认为gotyeswf
				"width": "100%", //视频宽,如 '600px' 或者 '50%', 默认'100%'
				"height": "350px", //视频高,如 '400px' 或者 '50%', 默认'100%'
				"autoplay": true, //默认true,是否自动播放
				"defFlv": false, //默认false,为rtmp协议
				"forceLogoutMsg": "Your token is used elsewhere", //被踢下线通知
				"showLoading": 1, //swf播放器是否显示加载动画; 默认显示; 1-显示 0-不显示.
			};
			var player = new Gotye.Player(token, opts);
			player.appendTo("playerWapper");
		};

		//直播测试
		$scope.livetest = function() {
			//获取token
			var params = new Object();
			Stest.get_live_data(params,function(data) {
				//根据token创建播放器
				$scope.makeplayer(data.result.token);
			})
		};
	}

	controller.$inject = ['$scope', 'Stest', '$window', '$timeout', 'GoPage', 'GoDialog', 'GoPicture', 'FileSystem'];

	return app.registerController("stestctrl", controller);
});